//
//  SAPSearchDetailTableViewCell.m
//  CarsHome
//
//  Created by dapeng on 15/12/18.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPSearchDetailTableViewCell.h"

@implementation SAPSearchDetailTableViewCell
- (void)dealloc {
    [_fctname release];
    [_twoModel release];
    [_img release];
    [_title release];
    [_prices release];
    [_level release];
    
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.img = [[UIImageView alloc] init];
        self.title = [[UILabel alloc] init];
        self.prices = [[UILabel alloc] init];
        self.level = [[UILabel alloc] init];
        self.fctname = [[UILabel alloc] init];
        [_fctname release];
        [_img release];
        [_title release];
        [_prices release];
        [_level release];
        [self addSubview:self.img];
        [self addSubview:self.title];
        [self addSubview:self.prices];
        [self addSubview:self.fctname];
        [self addSubview:self.level];
        [_fctname release];
        [_img release];
        [_title release];
        [_prices release];
        [_level release];
      
    }return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat fctWidth = [self getWidthWithText:self.fctname.text withFont:15];
    self.img.frame = CGRectMake(10, 10, SCREEN_SIZE.width / 4, SCREEN_SIZE.height / 8 - 20);
    self.fctname.frame = CGRectMake(SCREEN_SIZE.width / 4 + 20, 10, fctWidth, 30);
    self.title.frame = CGRectMake(fctWidth + SCREEN_SIZE.width / 4 + 20, 10, SCREEN_SIZE.width - fctWidth + SCREEN_SIZE.width / 4 + 20, 30);
    self.level.frame = CGRectMake(SCREEN_SIZE.width / 4 + 20, SCREEN_SIZE.height / 8 - 30, 80, 20);
    self.prices.frame = CGRectMake(SCREEN_SIZE.width - 100, SCREEN_SIZE.height / 8 - 30, 100, 20);
    
}
- (void)setTwoModel:(SAPFindCarTwoModel *)twoModel {
    if (_twoModel != twoModel) {
        [_twoModel release];
        _twoModel = [twoModel retain];
    }
    [self.img sd_setImageWithURL:[NSURL URLWithString:twoModel.imgurl]];
    self.title.text = twoModel.name;
    self.prices.text = twoModel.pricebetween;
    self.level.text = twoModel.level;
    self.fctname.text = [NSString stringWithFormat:@"%@-", twoModel.fctname];
    
    _fctname.font = [UIFont systemFontOfSize:15];
    _title.font = [UIFont systemFontOfSize:15];
    _level.font = [UIFont systemFontOfSize:12];
    _prices.font = [UIFont systemFontOfSize:12];
    _level.textColor = [UIColor grayColor];
    _prices.textColor = [UIColor redColor];

    NSRange range = [self.title.text rangeOfString:self.searchText];
    NSMutableAttributedString *heightLightString = [[NSMutableAttributedString alloc] initWithString:self.title.text];
    [heightLightString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range];
    self.title.attributedText = heightLightString;
    

}


- (CGFloat)getWidthWithText:(NSString *)text withFont:(CGFloat)font {
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.width;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
